package prassein.rsm.web;

import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.Constants;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({
	@Result(name = "success",location="resumeInsert.jsp"), 
	@Result(name = "error",location="login.jsp"),
	@Result(name = "input",location="resumeInsert.jsp")})
public class ResumeEditAction extends ActionSupport {
	
	private static final long serialVersionUID = -793118724462377480L;
	
	@Autowired
	private ResumeManager resumeManager;
	
	private Resume resume = new Resume();
	
	private int resumeId = -1;
	
	private int submitResult = -1;
	
	@Override 
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (resumeId > -1) {
			try {
				resume = resumeManager.getResume(resumeId);
				return SUCCESS;
				
			} catch (Exception e) {
				submitResult = 2;
				return INPUT;
			}
		} else if (session.get(Constants.USERNAME) != null && !session.get(Constants.USERNAME).equals("")) {
			try {
				resume = resumeManager.getResume(Integer.parseInt(session.get(Constants.LATEST_EDIT_REUMEID).toString()));
				return SUCCESS;
				
			} catch (Exception e) {
				submitResult = 2;
				return INPUT;
			}
		} else {
			return ERROR;
		}
	}
	
	public ResumeManager getResumeManager() {
		return resumeManager;
	}

	public void setResumeManager(ResumeManager resumeManager) {
		this.resumeManager = resumeManager;
	}

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public int getResumeId() {
		return resumeId;
	}

	public void setResumeId(int resumeId) {
		this.resumeId = resumeId;
	}

	public int getSubmitResult() {
		return submitResult;
	}

	public void setSubmitResult(int submitResult) {
		this.submitResult = submitResult;
	}
}
